/* 
 * File:   Streamlines.cpp
 * Author: a1574256
 * 
 * Created on January 18, 2013, 3:17 PM
 */

#include "Ducks.h"
#include "GL/gl.h"

using namespace std;

Ducks::Ducks()
:
    Visualizer(),
    d_positions(0),
    d_colors(0),
    d_dt(10)

{   
    d_uses_colormap = true;
    d_uses_vector_dataset = true;
    d_current_map_dataset_index = int(V);
    d_current_vector_dataset_index = int(Flow);
    d_positions.push_back({Vector2f(25.0, 25.0)});
    d_colors.push_back({0.0});
}

void Ducks::update()
{
    for (int sline = 0; sline != d_positions.size(); ++sline)
    {
        Vector2f curPos = d_positions[sline].back();        
        Vector2f v = d_vector_dataset->getData(curPos.x, curPos.y);                
        
        Vector2f newPos = curPos + v * d_dt;
        d_positions[sline].push_back(newPos);
        
        std::cout << curPos << ' ' << newPos << '\n';
        
        double col = d_map_dataset->getData(curPos.x, curPos.y);
        d_colors[sline].push_back(col);
    }
}

void Ducks::draw(int winW, int winH)
{
    
    const int DIM = d_simulation->getDim();
    fftw_real  wn = (fftw_real)winW / (fftw_real)(DIM + 1);   // Grid cell width
    fftw_real  hn = (fftw_real)winH / (fftw_real)(DIM + 1);  // Grid cell heigh
    
    cout << "First: " << d_positions[0][0] << '\n';
    
    setColormapMinMax();
    
    for (int sline  = 0; sline != d_colors.size(); ++sline)
        for (int t = 0; t != d_colors[sline].size() - 1; ++t)
        {
            d_map.set(d_colors[sline][t+1]);
            Vector2f fstPos = d_positions[sline][t];
            Vector2f sndPos = d_positions[sline][t+1];
            
            glBegin(GL_LINES);
                glVertex2f(wn + fstPos.x * wn, hn + fstPos.y * hn);
                glVertex2f(wn + sndPos.x * wn, hn + sndPos.y * hn);
            glEnd();
        }
}       

void Ducks::addSeed(float x, float y)
{
    d_positions.push_back({Vector2f(x, y)});
}



